package com.michale.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.michale.admin.controller.tool.BaseController;
import com.michale.common.annotation.Log;
import com.michale.common.constants.UserConstant;
import com.michale.common.domain.system.PostEntity;
import com.michale.common.enmus.BusinessType;
import com.michale.common.result.AjaxResult;
import com.michale.common.result.PageResult;
import com.michale.system.service.PostService;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Michale
 * @apiNote 岗位管理
 */
@Api(tags = "岗位管理")
@RestController
@RequestMapping("system/post")
public class PostController extends BaseController {

    @Resource
    private PostService postService;

    /**
     * 分页查询岗位列表
     *
     * @param postEntity 分页查询参数
     * @return
     */
    @GetMapping("/postList")
    @PreAuthorize("@security.isPermission('system:post:list')")
    public PageResult getPostList(PostEntity postEntity) {
        startPage();
        List<PostEntity> postList = postService.selectPostList(postEntity);
        PageInfo<PostEntity> pageInfo = new PageInfo<>(postList);
        return pageResult(pageInfo);
    }

    /**
     * 查询-岗位详细信息
     *
     * @param postId 岗位ID
     * @return 岗位详细信息
     */
    @GetMapping("/{postId}")
    @PreAuthorize("@security.isPermission('system:post:query')")
    public AjaxResult getInfoList(@PathVariable Long postId) {
        return AjaxResult.success(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     *
     * @param postEntity 岗位信息
     * @return 结果
     */
    @PostMapping("/insertPost")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PreAuthorize("@security.isPermission('system:post:add')")
    public AjaxResult insertPost(@Validated @RequestBody PostEntity postEntity) {
        if (UserConstant.NOT_UNIQUE.equals(postService.checkPostNameUnique(postEntity))) {
            return AjaxResult.error("新增岗位'" + postEntity.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstant.NOT_UNIQUE.equals(postService.checkPostCodeUnique(postEntity))) {
            return AjaxResult.error("新增岗位'" + postEntity.getPostName() + "'失败，岗位编码已存在");
        }
        return toAjax(postService.insertPost(postEntity));
    }

    /**
     * 修改岗位信息
     *
     * @param postEntity 岗位信息
     * @return 结果
     */
    @PutMapping("/updatePost")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@security.isPermission('system:post:edit')")
    public AjaxResult updatePost(@Validated @RequestBody PostEntity postEntity) {
        if (UserConstant.NOT_UNIQUE.equals(postService.checkPostNameUnique(postEntity))) {
            return AjaxResult.error("修改岗位'" + postEntity.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstant.NOT_UNIQUE.equals(postService.checkPostCodeUnique(postEntity))) {
            return AjaxResult.error("修改岗位'" + postEntity.getPostName() + "'失败，岗位编码已存在");
        }
        return toAjax(postService.updatePost(postEntity));
    }

    /**
     * 根据岗位ID删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    @DeleteMapping("/deletePost/{postIds}")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@security.isPermission('system:post:remove')")
    public AjaxResult deletePost(@PathVariable Long[] postIds) {
        return toAjax(postService.deletePostByIds(postIds));
    }
}
